package com.ml.mall.utils;

import com.ml.mall.bean.bo.ipay.IpayNewBo;
import com.ml.mall.bean.bo.ipay.IpayQueryBo;
import com.ml.mall.consts.CommonConsts;
import com.ml.mall.entity.payinfo.SysPayinfo;
import com.ml.mall.entity.sys.SysUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.math.BigDecimal;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;


@Component
@Slf4j
public class SdkIpay88Utils {
    //后台URL
    private static String s_backendUrl;

    @Value("${dns.backend_url}")
    private String backendUrl;

    public static Map<String, String> doRequestToIpay88(SysPayinfo payinfo, SysUser user) throws NoSuchAlgorithmException {
        IpayNewBo IpayNewBo = new IpayNewBo();
        IpayNewBo.setPayType(CommonConsts.PAY_ORDER_TYPE_ORDER);
        IpayNewBo.setRefNo(payinfo.getPayId().toString());
        IpayNewBo.setAmount(payinfo.getMoney());
        IpayNewBo.setUserName(user.getName());
        IpayNewBo.setUserEmail(user.getEmail());
        IpayNewBo.setUserContact(user.getTelNumber());
        IpayNewBo.setProdDesc("There is description of product");
        return doRequestToIpay88(IpayNewBo);
    }

    @PostConstruct
    public void getS_backendUrl(){
        s_backendUrl = this.backendUrl;
    }

    private static Map<String,String> doRequestToIpay88(IpayNewBo bo) throws NoSuchAlgorithmException {
        String  responseUrl = s_backendUrl + "/order/ipay88/reposenUrl"; //支付完成跳转我方地址
        String  backendUrl  = s_backendUrl + "/order/ipay88/callback";   //我方接收异步地址
        HashMap<String,String> requestMap = new HashMap<>();
        requestMap.put("MerchantCode", CommonConsts.MERCHANTCODE);
        requestMap.put("RefNo",bo.getRefNo());
        requestMap.put("Currency","MYR");
        requestMap.put("ProdDesc",bo.getProdDesc());
        requestMap.put("UserName",bo.getUserName());
        requestMap.put("UserEmail",bo.getUserEmail());
        requestMap.put("UserContact",bo.getUserContact());
        requestMap.put("SignatureType","SHA256");
        requestMap.put("ResponseURL",responseUrl);
        requestMap.put("BackendURL",backendUrl);

//        测试数据，测试金额只能为1.00
//        bo.setAmount(new BigDecimal(1.00));

        //计算加密
//        BigDecimal;保留两位小数转成千位
        BigDecimal amountDecimal  = bo.getAmount().multiply(new BigDecimal(100));
        Long amountLong = amountDecimal.setScale(2, BigDecimal.ROUND_HALF_UP).longValue();

        requestMap.put("Amount",String.format("%.2f", bo.getAmount())); //两位小数
        String paramStr =  CommonConsts.MERCHANTKEY + CommonConsts.MERCHANTCODE + bo.getRefNo() + amountLong + "MYR";
        log.info("paramStr====={}",paramStr);
        String sign = SHA256.calculateSha256(paramStr);
        requestMap.put("Signature",sign);
        log.info("requestMap========={}",requestMap);
        //组织数据放入查询列表
        IpayQueryBo queryBo = new IpayQueryBo();
        queryBo.setAmount(bo.getAmount().toString());
        queryBo.setRefNo(bo.getRefNo());

        //返回数据不全；暂时只通过异步
//        Ipay88ServiceImpl obj = new Ipay88ServiceImpl();
//        obj.pushObjToRedisList(queryBo);

        return requestMap;
    }
}
